package com.syni.mdd.yhd.common.all.service;

import com.syni.mdd.yhd.common.all.dao.jpa.BmsDxAnswerDao;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsbusinessDao;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.BmsDxAnswer;
import com.syni.mdd.yhd.common.all.entity.BmsDxComment;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @className BmsUserOperationLogService
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsDxAnswerService extends BaseService<BmsDxAnswer, Integer> {

    @Autowired
    BmsDxAnswerDao bmsDxAnswerDao;

    @Autowired
    BmsDxCommentService bmsDxCommentService;

    @Override
    public BmsDxAnswerDao getBaseDao() {
        return bmsDxAnswerDao;
    }

    public List<BmsDxAnswer> findCommentAnswer(Set commpentIds){
        return bmsDxAnswerDao.findByBmsDxCommentIdIn(commpentIds);
    }

    public List<BmsDxAnswer> findByAnswerIds(Set answerIds){
        return bmsDxAnswerDao.findAllById(answerIds);
    }

    public Map<Integer,Integer> countVideoAnswer(Set<Integer> videoIds) {
        Map<Integer,Integer> resultMap = new HashMap<>();
        //找出video的评论
        Map<Integer,Integer> videoCommentMap = new HashMap<>();
        List<BmsDxComment> bmsDxComments = bmsDxCommentService.getBaseDao().findByBmsVideoIdIn(videoIds);
        Set<Integer> commentIds = new HashSet<>();
        if(bmsDxComments != null && bmsDxComments.size() > 0){
            for(BmsDxComment bmsDxComment : bmsDxComments){
                commentIds.add(bmsDxComment.getId());
                videoCommentMap.put(bmsDxComment.getId(),bmsDxComment.getBmsVideoId());
            }
        }
        if(commentIds.size() > 0){
            List<Map<String,Object>> videoAnswerList = bmsDxAnswerDao.countVideoAnswerNum(commentIds);
            if(videoAnswerList != null && videoAnswerList.size() > 0){
                for(Map<String,Object> objectMap : videoAnswerList){
                    Integer bmsDxCommentId = Integer.parseInt(objectMap.get("bms_dx_comment_id").toString());
                    Integer num = Integer.parseInt(objectMap.get("num").toString());
                    if(videoCommentMap.containsKey(bmsDxCommentId)){
                        Integer videoId = videoCommentMap.get(bmsDxCommentId);
                        if(!resultMap.containsKey(videoId)){
                            resultMap.put(videoId,0);
                        }
                        resultMap.put(videoId,resultMap.get(videoId) + num);
                    }
                }
            }
        }
        return resultMap;
    }
}
